// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Graph.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.Numerics.GraphOptimization
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using ShoNS.Array;
    
    /// <summary>
    /// Graph class
    /// </summary>
    /// <typeparam name="NodeType">Object that is stored at each node in the graph</typeparam>
    /// <typeparam name="EdgeType">Type of edge in between in each node</typeparam>
    public class Graph<NodeType, EdgeType> where EdgeType : Edge<NodeType>
    {
        /// <summary>
        /// Initializes a new instance of the Graph class
        /// </summary>
        public Graph()
        {
            this.Nodes = new List<NodeType>();
            this.Edges = new List<EdgeType>();
        }

        /// <summary>
        /// Gets the nodes of the graph
        /// </summary>
        public List<NodeType> Nodes { get; private set; }

        /// <summary>
        /// Gets the edges of the graph
        /// </summary>
        public List<EdgeType> Edges { get; private set; }

        /// <summary>
        /// Adds a node to the graph
        /// </summary>
        /// <param name="node">the node to add</param>
        public void AddNode(NodeType node)
        {
            this.Nodes.Add(node);
        }

        /// <summary>
        /// Adds an edge to the graph
        /// </summary>
        /// <param name="edge">the edge to add</param>
        public void AddEdge(EdgeType edge)
        {
            this.Edges.Add(edge);
        }

        /// <summary>
        /// Override of the ToString() method
        /// </summary>
        /// <returns>formatted string</returns>
        public override string ToString()
        {
            return string.Format("Number of nodes = {0}\nNumber of edges = {1}", this.Nodes.Count, this.Edges.Count);
        }

        /// <summary>
        /// Optimize the graph
        /// </summary>
        /// <param name="error">Pointer to error function</param>
        /// <param name="increment">Pointer to increment function</param>
        public void Optimize(LinearSystem<NodeType, EdgeType>.ErrorFunction error, LinearSystem<NodeType, EdgeType>.IncrementFunction increment)
        {
            // TODO: add learning rate scheduler and convergence check
            for (int iteration = 0; iteration < 5; iteration++)
            {
                Console.WriteLine("Iteration {0}", iteration);
                LinearSystem<NodeType, EdgeType> system = new LinearSystem<NodeType, EdgeType>(this, error, increment);
              
                DoubleArray update = system.Solve();

                int dim = this.Edges[0].Dimensions;
                double[] inc = new double[dim];
                for (int i = 1; i < this.Nodes.Count; i++)
                {
                    for (int j = (i - 1) * 3, count = 0; j < (i - 1) * 3 + dim; j++, count++)
                    {
                        inc[count] = -update[j];
                    }

                    this.Nodes[i] = increment(this.Nodes[i], inc);
                }

                update.Dispose();
            }
        }
    }
}
